package com.fuqi.android.zhongcai.business.shuig.view.activity;


import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.Constants;
import com.fuqi.android.zhongcai.base.request.api.GetStaffApi;
import com.fuqi.android.zhongcai.base.request.result.GetStaffResult;
import com.fuqi.android.zhongcai.base.view.activity.BaseActivity;
import com.fuqi.android.zhongcai.base.view.widget.wheelwidget.OptionsPickerView;
import com.fuqi.android.zhongcai.business.shuig.request.api.OrderHandleApi;
import com.fuqi.android.zhongcai.business.shuig.request.result.OrderHandleResult;
import com.fuqi.android.zhongcai.business.shuig.view.widget.CJRow.ARResponse;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.android.zhongcai.utils.FormParamsUtils;
import com.fuqi.android.zhongcai.utils.LogUtils;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.Bind;
import butterknife.OnClick;

import static com.fuqi.android.zhongcai.business.shuig.OrderDict.StatusType.DDZT_DCL;

public class ShuiGPaiDanActivity extends BaseActivity implements FormParamsUtils.RequestCallback{


    @Bind(R.id.tv_nq_paidan)
    TextView tvNQpaidan;

    private ArrayList<GetStaffResult> options1Items = new ArrayList<GetStaffResult>();
    OptionsPickerView<GetStaffResult> pvOptions;
    ARResponse response;

    @OnClick({R.id.btn_paidan,R.id.rl_paidan})
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.btn_paidan:

                String dzName = tvNQpaidan.getText().toString().trim();
                if (dzName.equals("") || dzName.equals("请选择")){

                    ActivityUtils.toast("请选择施工部");

                } else{

//                    FormParamsUtils.getInstance(ShuiGPaiDanActivity.this).request(new OrderHandleApi.OrderHandleParams("ORDER_ID","ORDER_STATUS",
//                            "CAPTAIN_ID"));
                    if(response != null) {
                        response.update("CAPTAIN_NAME", tvNQpaidan.getText().toString());
                        response.update("CAPTAIN_ID", tvNQpaidan.getTag().toString());
                        response.update("ORDER_STATUS", DDZT_DCL);
                        RequestManager.getInstance().call(new OrderHandleApi(
                                new OrderHandleApi.OrderHandleParams(response.getDataWithMap()), respOrderListener, errorListener));
                        Log.i("response : ", response.toString());
                    }
                }

                break;

            case R.id.rl_paidan:
                pvOptions = new OptionsPickerView(this);
                pvOptions.setPicker(options1Items);
                pvOptions.setCyclic(false);
                pvOptions.show();
                pvOptions.setSelectOptions(1, 0, 0);
                pvOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {

                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3) {
                        if(options1Items.size() <= options1)       return ;
                        tvNQpaidan.setText(options1Items.get(options1).getName());
                        tvNQpaidan.setTag(options1Items.get(options1).getUserId());
                        //ActivityUtils.toast("options1: " + options1);
                        LogUtils.i("juner select position: " + options1);

                    }
                });

                break;


        }

    }


    @Override
    public void initView() {

    }

    @Override
    public void initListener() {

    }

    Handler h = new Handler(){

    };

    @Override
    public void initData() {
        titleBar.setTitle("派单");
        response = new ARResponse(getIntent().getStringExtra("response"));
        RequestManager.getInstance().call(new GetStaffApi(new GetStaffApi.GetStaffParams(Constants.ROLE_SGDZ), respWorkersListener, errorListener));

    }


    Response.Listener<OrderHandleResult> respOrderListener = new Response.Listener<OrderHandleResult>() {

        @Override
        public void onResponse(OrderHandleResult response) {
            if (HandleCode.requestSuccess()) {
                finish();
                Log.i("juner", "juner respListener response : ");
            } else {

            }
        }
    };

    Response.Listener<GetStaffResult[]> respWorkersListener = new Response.Listener<GetStaffResult[]>() {

        @Override
        public void onResponse(GetStaffResult[] response) {
            if (HandleCode.requestSuccess()) {
                if (response != null && response.length > 0){
                    options1Items.clear();
                    Collections.addAll(options1Items, response);
                }
                Log.i("juner", "juner respListener response : ");
            } else {

            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());
        }
    };

    @Override
    public boolean hasTitleBar() {
        return true;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_nqpai_dan;
    }

    @Override
    public void onUploadStart() {

    }

    @Override
    public void onUploadFinish() {

    }

    @Override
    public void onUploadSuccess() {

        finish();

    }

    @Override
    public void onUploadError() {

    }
}
